<?

Prado::using('Application.pages.utils.CommonUtils');

class ImageContainer extends TPage
{
	public $found = "true";
	// Navigated ID
	public $id = -1;
	
	public function onInit($param)
    {
//		parent::onInit($param);
        if(!$this->IsPostBack)  // if the page is requested the first time
        {
			
		}
		$this->id = $this->Request['id'];
		$this->setTitle($this->getCurrentRecord()->username ."'s Images");
		
		$this->Repeater_imageItem->VirtualItemCount = ImageCntRecord::finder()->count('user_id = ?',$this->id);
		
		$this->populateImagesCnt();
		$this->verifyOwner();
	}
	
	public function verifyOwner()
	{
		if($this->isNavigatedUserSameAsOwner())
		{ // showing owner profile...
		
			
		}			
		else // showing someone else profile
		{
			// hide upload
			$this->uploadPanel->Visible = false;
		}
	}
	
	
	public function isNavigatedUserSameAsOwner()
	{
		return $this->id == $this->user->ID;
	}
	
	public function populateImagesCnt()
	{
		$offset=$this->Repeater_imageItem->CurrentPageIndex*$this->Repeater_imageItem->PageSize;
        $limit=$this->Repeater_imageItem->PageSize;
        if($offset + $limit > $this->Repeater_imageItem->VirtualItemCount)
            $limit=$this->Repeater_imageItem->VirtualItemCount-$offset;
        $this->Repeater_imageItem->DataSource=$this->getRequests_ImageCnt($offset,$limit);
        $this->Repeater_imageItem->dataBind();
	}
	protected function getRequests_ImageCnt($offset, $limit)
    {
        // Construts a query criteria
        $criteria = new TActiveRecordCriteria;		
		$criteria->Condition = 'user_id = :id';
		$criteria->Parameters[':id'] = $this->id;
        
        $criteria->Limit=$limit;
        $criteria->Offset=$offset;
	//	echo $criteria;
		//return;
        // query for the posts with the above criteria and with author information
        return ImageCntRecord::finder()->findAll($criteria);
    }
	
	public function pageChanged_ImageCnt($sender,$param)
    {
        // change the current page index to the new one
        $this->Repeater_imageItem->CurrentPageIndex=$param->NewPageIndex;
        // re-populate data into the repeater
        $this->populateImagesCnt();
    }
	
	// for fileUploaded
	public function fileUploaded($sender,$param)
    {
        if($sender->HasFile)
        {
			$size = $sender->FileSize / 1024;
			if($size > 500)
				$this->Shout("Error : file size exceeded the limits (500 KB)");
			else
			{
				//**** at server used >>>	$path = $this->Application->AssetManager->getBasePath() ."/images/". $this->id ."/cnt/";
				$img_folder = $this->Application->Parameters['ImageFolderName'];
				$path = $this->Application->getBasePath() . "\\..\\". $img_folder ."\\".$this->id."\\cnt\\";
				
				// if directory is not exists >> Create it
				if(is_dir($path) == null)
				{
					mkdir($path,0777,true);;
				}
					
				echo " Is dir >> " . is_dir($path);
				
				// rename algorithm...
				
//				$uniqueFileName = $sender->FileName;
//				if(is_file($path . $sender->FileName))
//					$uniqueFileName = CommonUtils::getUniqueFileName($sender->FileName);
				if(!is_file($path . $sender->FileName))	
				{
					$sender->saveAs($path . $sender->FileName);
					$this->updateImagesDB($sender->FileName);
				}
				else
				{
					$this->Shout("Error : This file is already exists in your container !");
				}
				
				
			}
        }
		else
			$this->Shout("Error : file doesn't exists");
			
		// updated the # on screen
		$this->onInit($param);	
    }
	
	public function updateImagesDB($filename)
	{
		$rec = new ImageCntRecord();
		// use Owner ID ...
		$rec->user_id = $this->user->ID;
		$rec->ImagePath = $filename;
		$rec->ImageDesc = "";
		$rec->save();	
	}
	
	public function getAvatar($user)
	{
	
		return CommonUtils::getAvatar($user);	
	}
	
	
	public function getOwnerRecord()
	{
	
	//	$userRecord = UserRecord::finder()->findByEmail($this->user->Email); $this->id
		$userRecord = UserRecord::finder()->findByUser_Id($this->user->ID);
		return $userRecord;
	}
	
	public function getCurrentRecord()
	{
	
	//	$userRecord = UserRecord::finder()->findByEmail($this->user->Email); $this->id
		$userRecord = UserRecord::finder()->findByUser_Id($this->id);
		return $userRecord;
	}
	
	public function Shout($msg)
	{
		$this->getPage()->getClientScript()->registerEndScript('', "genericMSG('$msg')");
	}
}
?>